package com.app.campusguide;

import java.util.ArrayList;
import com.app.sqlite_connect.HandlerSqlite;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import android.app.Activity;
import android.content.Intent;

public class Delete_class_activity extends Activity {
	private final String APP_LOG = "Delete_class_activity";
	private HandlerSqlite database;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		database = new HandlerSqlite(this);
		setContentView(R.layout.activity_delete_class);
		
		//Clean values of views
		EditText etext_class_name = (EditText)findViewById(R.id.etxt_delete_class_name_class);
		etext_class_name.setEnabled(false);
		etext_class_name.setText("");
		Spinner spn_start_hour = (Spinner)findViewById(R.id.spn_delete_class_start_hour);
		spn_start_hour.setAdapter(null);
		
		ImageView btn_delete_class_home =(ImageView) findViewById(R.id.img_btn_delete_class_home);
		btn_delete_class_home.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	Intent i = new Intent(getApplicationContext(),AcivityMain.class);
        		startActivity(i);
            }      
        });
		
		initDayOfWeekSpinner();
		initStartHourSpinner();
		deleteButtonSetUp();
	}
	
	/**
	 * This method allow to set up values of day of week spinner and their actions when an item was selected 
	 */
	private void initDayOfWeekSpinner()
	{
		Spinner spn_day_of_week = (Spinner)findViewById(R.id.spn_delete_class_day_of_week);
		ArrayAdapter<?> adp_day_of_week_adapter = ArrayAdapter.createFromResource(this, R.array.days_name,android.R.layout.simple_spinner_item);
		adp_day_of_week_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spn_day_of_week.setAdapter(adp_day_of_week_adapter);
		spn_day_of_week.setOnItemSelectedListener(new OnItemSelectedListener(){
			public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id){
				//Get id of day selected
				String str_day_of_week = ((Long)parentView.getItemIdAtPosition(position)).toString();
				//Query for start hour class to take this day
				ArrayList<String> str_array_start_hours = database.queryStartHourByDay(str_day_of_week);
				//Clean start hour spinner 
				Spinner spn_start_hour = (Spinner)findViewById(R.id.spn_delete_class_start_hour);
				spn_start_hour.setAdapter(null);
				//Clean class name Edit Text 
				EditText etext_class_name = (EditText)findViewById(R.id.etxt_delete_class_name_class);
				etext_class_name.setText("");
				//Fill spinner with result of query to database
				ArrayAdapter<?> adapter_hours = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, str_array_start_hours);
				adapter_hours.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				spn_start_hour.setAdapter(adapter_hours);
			}
			
			public void onNothingSelected(AdapterView<?> parentView){
			}	
		});
	}
	
	/**
	 * This method allow to set up actions when an item of start hour spinner was selected
	 */
	private void initStartHourSpinner()
	{
		Spinner spn_start_hour = (Spinner)findViewById(R.id.spn_delete_class_start_hour);
		spn_start_hour.setOnItemSelectedListener(new OnItemSelectedListener(){
			public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id){
				//Get hour selected
				String str_start_hour = parentView.getItemAtPosition(position).toString();
				//Get day selected
				Spinner spn_day_of_week = (Spinner)findViewById(R.id.spn_delete_class_day_of_week);
				String str_day_of_week = ((Long)parentView.getItemIdAtPosition(spn_day_of_week.getSelectedItemPosition())).toString();
				//Query for class
				String str_class_name = database.queryClassNameByDayAndStartHour(str_day_of_week, str_start_hour);
				//Assign value to edit text
				EditText etext_class_name = (EditText)findViewById(R.id.etxt_delete_class_name_class);
				etext_class_name.setText(str_class_name);
			}
			
			public void onNothingSelected(AdapterView<?> parentView){
			}	
		});
	}
	
	/**
	 * This method set up actions for ok button
	 */
	private void deleteButtonSetUp(){
		Button btn_delete_class_ok =(Button) findViewById(R.id.btn_delete_class_ok);
		btn_delete_class_ok.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	EditText etext_class_name = (EditText)findViewById(R.id.etxt_delete_class_name_class);
            	Spinner spn_start_hour = (Spinner)findViewById(R.id.spn_delete_class_start_hour);
            	Spinner spn_day_of_week = (Spinner)findViewById(R.id.spn_delete_class_day_of_week);
            	String str_day_of_week =  ((Long)spn_day_of_week.getItemIdAtPosition(spn_day_of_week.getSelectedItemPosition())).toString();;
            	String str_class_name = etext_class_name.getText().toString();
          
            	//Verify if all fields are fill
            	if(str_day_of_week.isEmpty() || str_class_name.isEmpty() || spn_start_hour.getItemAtPosition(spn_start_hour.getSelectedItemPosition()) == null){
            		TextView text1 = new TextView(getApplicationContext());
    		    	text1.setText(R.string.str_most_asign_values);
    		    	showToast (text1.getText().toString());
    		    	text1 = null;
    		    	return;
            	}
            	
            	String str_star_hour = spn_start_hour.getItemAtPosition(spn_start_hour.getSelectedItemPosition()).toString();;
            	
            	if(database.deleteRowOnSchedule(str_class_name, str_day_of_week, str_star_hour)){
            		TextView text1 = new TextView(getApplicationContext());
    		    	text1.setText(R.string.str_successful_process);
    		    	showToast (text1.getText().toString());
    		    	text1 = null;
    		    	spn_start_hour.setSelection(0);
    		    	etext_class_name.setText("");
    		    	spn_day_of_week.setSelection(0);
            	}else{
            		TextView text1 = new TextView(getApplicationContext());
    		    	text1.setText(R.string.str_unexpected_error);
    		    	showToast (text1.getText().toString());
    		    	text1 = null;
            	}
            }      
        });
	}
	
	/**
	 * This method show an alert message with Toast class
	 * @param str_message String message to show on Toast
	 */
	private void showToast(String str_message){
		Toast toast = Toast.makeText(getApplicationContext(), str_message, Toast.LENGTH_LONG);
		View textView = toast.getView();
		LinearLayout lay = new LinearLayout(getApplicationContext());
		lay.setOrientation(LinearLayout.HORIZONTAL);
		ImageView view = new ImageView(getApplicationContext());
		view.setImageResource(android.R.drawable.ic_menu_info_details);
		lay.addView(view);
		lay.addView(textView);
		toast.setView(lay);
		toast.show();
	}
}
